package org.ithasu.tools;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class ConfigTools {

	private static final Properties properties = new Properties();

	protected ConfigTools() {
	}

	public static String getProperty(String propertyName) {
		return properties.getProperty(propertyName);
	}

	public static void setProperty(String propertyName, String value) {
		properties.setProperty(propertyName, value);
	}

	// -- Persistence ----------------------------------------

	public static String getConfigFilename(String appName) {
		return System.getProperty("user.home") + "/." + appName;
	}

	public static void load(String appName) {
		try {
			InputStream stream = new FileInputStream(getConfigFilename(appName));
			properties.load(stream);
		} catch (Exception e) {
			Console.error(e);
		}
	}

	public static void save(String appName) {
		try {
			OutputStream stream = new FileOutputStream(getConfigFilename(appName));
			properties.store(stream, appName + " - " + TimeTools.format(TimeTools.getLocalTime()));
		} catch (Exception e) {
			Console.error(e);
		}

	}

}
